﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace JOthello_SRV
{
    public static class Output
    {
        /// <summary>
        /// giebt an ob verbose output aktiviert ist
        /// </summary>
        public static bool Verbose;

        private static string _LogDir;
        private static String _LogFilePath;

        public static string WarningPrefix
        {
            get
            {
                return "[WARNING]\t";
            }
        }
        public static string ErrorPrefix
        {
            get
            {
                return "[ERROR]\t";
            }
        }

        #region getter / setter

        public static string LogDir
        {
            get
            {
                return _LogDir;
            }
            set
            {
                _LogDir = value;
                if (!Directory.Exists(_LogDir)) Directory.CreateDirectory(_LogDir);
                Logpath = _LogDir + Path.DirectorySeparatorChar + "JOthello.log";
            }

        }
        private static string Logpath
        {
            set
            {
                _LogFilePath = value;
            }
        }
        public static bool isLogging
        {
            get
            {
                return !string.IsNullOrWhiteSpace(Output.LogDir);
            }
        }
        #endregion

        /// <summary>
        /// giebt einen string auf der konsole aus, evtl nur falls verbose = true
        /// </summary>
        /// <param name="s"></param>
        public static void Write(string s, bool justIfVerbose = false)
        {
            if (Output.Verbose || !justIfVerbose)
            {
                System.Console.Write(s);
                Log(s);
            }
        }

        /// <summary>
        /// giebt einen string auf der konsole aus, evtl nur falls verbose = true, und beginnt eine neue zeile
        /// </summary>
        /// <param name="s"></param>
        public static void WriteLine(string s, bool writeNameAfter = true, bool justIfVerbose = false, bool appendToLog = true)
        {
            if (Output.Verbose || !justIfVerbose)
            {
                System.Console.WriteLine(s);
                if (writeNameAfter) Output.WriteName(justIfVerbose);

                if(appendToLog) Log(s);
            }
        }

        /// <summary>
        /// giebt einen fehler aus
        /// </summary>
        /// <param name="s"></param>
        public static void WriteError(string s)
        {
            ConsoleColor tmp = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Error.WriteLine(ErrorPrefix + s);
            Console.ForegroundColor = tmp;

            Log(ErrorPrefix + s);
        }

        /// <summary>
        /// giebt eine Warnung aus
        /// </summary>
        /// <param name="s"></param>
        public static void WriteWarning(string s, bool justIfVerbose = false)
        {
            if (Output.Verbose || !justIfVerbose)
            {
                ConsoleColor tmp = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Yellow;
                System.Console.WriteLine(WarningPrefix + s);
                Console.ForegroundColor = tmp;

                Log(WarningPrefix + s);
            }
        }

        /// <summary>
        /// schreibt den angegebenen string in das logfile
        /// </summary>
        /// <param name="msg"></param>
        private static void Log(string msg)
        {
            try
            {
                if (_LogFilePath != null)
                {
                    File.AppendAllText(
                            _LogFilePath,
                            "[ " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + " ]\t" + msg + Environment.NewLine,
                            Encoding.Default
                        );
                }
            }
            catch (Exception e)
            {
#if DEBUG
                Debug.Print(e.StackTrace);
#endif
            }
        }

        /// <summary>
        /// schreibt den namen des programmes auf einen neue zeile
        /// </summary>
        /// <param name="justIfVerbose"></param>
        public static void WriteName(bool justIfVerbose = false)
        {
            if(Verbose || !justIfVerbose)
                System.Console.Write(Environment.NewLine + Application.ProductName + "> ");
        }
    }
}
